package com.ten.mall.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.Date;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.ten.mall.vo.OrderReturnVo;

public class PDFReport {
	Document document = new Document();// 建立一个Document对象 
	private static Font headfont ;// 设置字体大小 
    private static Font keyfont;// 设置字体大小 
    private static Font textfont;// 设置字体大小 
    static{ 
        BaseFont bfChinese; 
        try { 
            //bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); 
            bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); 
            headfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小 
            keyfont = new Font(bfChinese, 8, Font.BOLD);// 设置字体大小 
            textfont = new Font(bfChinese, 8, Font.NORMAL);// 设置字体大小 
        } catch (Exception e) {          
            e.printStackTrace(); 
        }  
    } 
    public PDFReport(File file) {         
        document.setPageSize(PageSize.A4);// 设置页面大小 
        try { 
           PdfWriter.getInstance(document,new FileOutputStream(file)); 
           document.open();  
       } catch (Exception e) { 
           e.printStackTrace(); 
       }  
             
   } 
    int maxWidth = 520; 
    public PdfPCell createCell(String value,Font font,int align){ 
        PdfPCell cell = new PdfPCell(); 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);         
        cell.setHorizontalAlignment(align);     
        cell.setPhrase(new Phrase(value,font)); 
       return cell; 
   } 
    private static PdfPCell createCell(String value,Font font){ 
        PdfPCell cell = new PdfPCell(); 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);  
        cell.setPhrase(new Phrase(value,font)); 
       return cell; 
   } 
    public PdfPCell createCell(String value,Font font,int align,int colspan){ 
        PdfPCell cell = new PdfPCell(); 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
        cell.setHorizontalAlignment(align);     
        cell.setColspan(colspan); 
        cell.setPhrase(new Phrase(value,font)); 
       return cell; 
   } 
    public PdfPCell createCell(String value,Font font,int align,int colspan,boolean boderFlag){ 
        PdfPCell cell = new PdfPCell(); 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
        cell.setHorizontalAlignment(align);     
        cell.setColspan(colspan); 
        cell.setPhrase(new Phrase(value,font)); 
        cell.setPadding(3.0f); 
        if(!boderFlag){ 
            cell.setBorder(0); 
            cell.setPaddingTop(15.0f); 
            cell.setPaddingBottom(8.0f); 
        } 
       return cell; 
   } 
    private static PdfPTable createTable(int colNumber){ 
        PdfPTable table = new PdfPTable(colNumber);
        int widths1[] ={2,8};
        try{ 
            table.setTotalWidth(520); 
            table.setWidths(widths1);
            table.setLockedWidth(true); 
            table.setHorizontalAlignment(Element.ALIGN_CENTER);      
            //table.getDefaultCell().setBorder(1); 
        }catch(Exception e){ 
            e.printStackTrace(); 
        } 
        return table; 
    } 
    
    public PdfPTable createTable(float[] widths){ 
        PdfPTable table = new PdfPTable(widths); 
        try{ 
            table.setTotalWidth(maxWidth); 
            table.setLockedWidth(true); 
            table.setHorizontalAlignment(Element.ALIGN_CENTER);      
            table.getDefaultCell().setBorder(1); 
        }catch(Exception e){ 
            e.printStackTrace(); 
        } 
        return table; 
    } 
    public PdfPTable createBlankTable(){ 
        PdfPTable table = new PdfPTable(1); 
        table.getDefaultCell().setBorder(0); 
        table.addCell(createCell("", keyfont));          
        table.setSpacingAfter(20.0f); 
        table.setSpacingBefore(20.0f); 
        return table; 
    } 
  
    
    public  static void createMyPdf(File file,OrderReturnVo vo) throws Exception{ 
    	Document dd =new Document(PageSize.A4, 20, 20, 20, 20);
    	PdfWriter.getInstance(dd,new FileOutputStream(file)); 
        dd.open(); 
    	PdfPTable table = createTable(2);
    	PdfPCell cell1 = createCell("订单号",keyfont);
    	PdfPCell cell2 = createCell(vo.getId(),keyfont);
    	PdfPCell cell3 = createCell("商品名称",keyfont);
    	PdfPCell cell4 = createCell(vo.getGoodsName(),keyfont);
    	PdfPCell cell5 = createCell("商品价格",keyfont);
    	PdfPCell cell6 = createCell(vo.getOrderPrice().toString(),keyfont);
    	PdfPCell cell7 = createCell("购买数量",keyfont);
    	PdfPCell cell8 = createCell(vo.getOrderSum().toString(),keyfont);
    	PdfPCell cell9 = createCell("微信昵称",keyfont);
    	PdfPCell cell10 = createCell(vo.getWeName(),keyfont);
    	PdfPCell cell11 = createCell("收货人",keyfont);
    	PdfPCell cell12 = createCell(vo.getReceiptName(),keyfont);
    	PdfPCell cell13 = createCell("联系电话",keyfont);
    	PdfPCell cell14 = createCell(vo.getTelephone(),keyfont);
    	PdfPCell cell15 = createCell("配送地址",keyfont);
    	PdfPCell cell16 = createCell(vo.getAddressDetail(),keyfont);
    	PdfPCell cell17 = createCell("下单时间",keyfont);
    	PdfPCell cell18 = createCell(CommonUtis.formateDate("yyyy-MM-dd HH:mm:ss",vo.getUpdateTime()),keyfont);
    	PdfPCell cell19 = createCell("配送人",keyfont);
    	PdfPCell cell20 = createCell("",keyfont);
    	PdfPCell cell21 = createCell("确认人",keyfont);
    	PdfPCell cell22 = createCell("",keyfont);
    	table.addCell(cell1);
    	table.addCell(cell2);
    	table.addCell(cell3);
    	table.addCell(cell4);
    	table.addCell(cell5);
    	table.addCell(cell6);
    	table.addCell(cell7);
    	table.addCell(cell8);
    	table.addCell(cell9);
    	table.addCell(cell10);
    	table.addCell(cell11);
    	table.addCell(cell12);
    	table.addCell(cell13);
    	table.addCell(cell14);
    	table.addCell(cell15);
    	table.addCell(cell16);
    	table.addCell(cell17);
    	table.addCell(cell18);
    	table.addCell(cell19);
    	table.addCell(cell20);
    	table.addCell(cell21);
    	table.addCell(cell22);
    	dd.add(table); 
    	dd.close();
    }
}
